<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="id?'编辑月卡':'增加月卡'" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form label-width="100px" ref="CarInfoForm" :rules="formRules" :model="CarInfoForm">
            <!-- ref=""验证 -->
            <el-form-item label="车主姓名" prop="personName">
              <el-input  v-model="CarInfoForm.personName"/>
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input  v-model="CarInfoForm.phoneNumber"/>
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input  v-model="CarInfoForm.carNumber"/>
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input  v-model="CarInfoForm.carBrand"/>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form label-width="100px" ref="feeForm" :rules="feeFormRules" :model="feeForm">
            <el-form-item label="有效日期" prop="payTime">
              <el-date-picker
              v-model="feeForm.payTime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yy-MM-dd"
              >
            </el-date-picker>
         </el-form-item>
         <el-form>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="feeForm.paymentAmount"/>
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="feeForm.paymentMethod">
                <el-option
                  v-for="item in ways"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value"/>
              </el-select>
            </el-form-item>  
          </el-form>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="confirmForm">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { CreateCardAPI, getDetailAPI, updateCardAPI } from '@/api/card'
export default {
  data(){
  const validatorCarNum=(rule,value,callback)=>{
    // console.log(rule,value,callback)
    // value:文本框输入的值
    // callback:回调函数
    const reg=/^[\u4E00-\u9FA5][\da-zA-Z]{6}$/
    if(!reg.test(value)){
      callback(new Error('请输入符合规则的车牌号'))
    }else{
      callback()
    }
  }
    return{
      CarInfoForm:{
        personName:'',
        phoneNumber:'',
        carNumber:'',
        carBrand:''
      },
      feeForm:{
        payTime:'',
        paymentAmount:'',
        paymentMethod:null//null代表全部
      },
      formRules:{
        personName:[
          //每一条规则对应一个对象
          //trigger:'change' 用户名或密码不能为空
          {required:true,message:'请输入车主姓名',trigger:'change'}
        ],
        phoneNumber:[
          {required:true,message:'请输入联系方式',trigger:'change'}
        ],
        carNumber:[
          {required:true,message:'请输入车辆号码',trigger:'change'},
          {validator:validatorCarNum,trigger:'change'}
        ],
        carBrand:[
          {required:true,message:'请输入车辆品牌',trigger:'change'}
        ]
      },
      feeFormRules:{
        payTime:[
          {required:true,message:'请输入有效日期',trigger:'change'}
        ],
        paymentAmount:[
          {required:true,message:'请输入支付金额',trigger:'change'}
        ],
        paymentMethod:[
          {required:true,message:'请输入支付方式',trigger:'change'}
        ]

      },
      //支付方式的下拉列表
      ways:[
        {value:'Aplipay',text:'支付宝'},
        {value:'WeChat',text:'微信'},
        {value:'Cash',text:'现金支付'}
      ],
      value:''
    }
  },
  computed:{
    id(){
      return this.$route.query.id
    }
  },
  mounted(){
    //钩子函数调用
    if(this.id){
      this.getDetailList()
    }
  },
  methods:{ 
    //获取数据
    async getDetailList(){
     const res= await getDetailAPI(this.id)
      console.log(res)
      const {carBrand,carNumber,personName,phoneNumber,carInfoId,rechargeId}=res.data
      this.CarInfoForm={carBrand,carNumber,personName,phoneNumber,carInfoId,rechargeId}
      const {cardEndDate,cardStartDate,paymentAmount,paymentMethod}=res.data
      this.feeForm.payTime=[cardEndDate,cardStartDate]
      this.feeForm.paymentAmount=paymentAmount
      this.feeForm.paymentMethod=paymentMethod
    },
    confirmForm(){
      this.$refs.CarInfoForm.validate((flag)=>{
        //如果第一个表单通过,验证第二个表单
        if(flag){
          this.$refs.feeForm.validate(async(flag)=>{
            if(flag){
              //开始调用接口添加数据
              const FormData={
                ...this.CarInfoForm,
                ...this.feeForm,
                cardStartDate:this.feeForm.payTime[0],
                cardEndDate:this.feeForm.payTime[1],
              }
              delete FormData.payTime
              if(this.id){
                await updateCardAPI(FormData)
              }else{
                await CreateCardAPI(FormData)
              }
              this.$message.success(`${this.id?'更新成功':'添加成功'}`)
             this.$router.back()
            }
          })
        }
      })
    },
    resetForm(){
      this.$refs.CarInfoForm.resetFields()
      this.$refs.feeForm.resetFields()

    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 40px 15px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
